package com.jams.goodox.activity.mine.address;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.activity.home.ChooseAddressFirstActivity;
import com.jams.goodox.constant.UrlConstants;
import com.jams.goodox.entity.Address;
import com.jams.goodox.listen.SuccessListener;
import com.jams.goodox.net.BaseEntity;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.HomeService;
import com.jams.goodox.net.services.MineService;
import com.jams.goodox.utils.ActionUtils;
import com.jams.goodox.utils.DataCacheManager;
import com.jams.goodox.utils.DialogUtils;
import com.jams.goodox.utils.EditTextUtils;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.utils.Utils;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * Created by Gu
 * Description 我的服务地址-修改
 */
public class MineAddressEditActivity extends BaseActivity {

    @Bind(R.id.input_box)
    EditText inputBox;
    @Bind(R.id.input_box2)
    EditText inputBox2;
    @Bind(R.id.input_box3)
    EditText inputBox3;
    @Bind(R.id.mine_address_submit)
    TextView mineAddressSubmit;
    @Bind(R.id.address)
    TextView address;
    @Bind(R.id.mask)
    ImageView mask_small;
    @Bind(R.id.parent_layout)
    RelativeLayout parentLayout;
    @Bind(R.id.mask_big)
    RelativeLayout mask; // 全屏的遮罩
    @Bind(R.id.address_default)
    TextView address_default;

    private Address bean;

    private String city;
    private boolean isChooseDefault;

    @Override
    protected int getLayoutId() {
        return R.layout.mine_address_edit;
    }

    @Override
    protected void init() {
//        StyleUtil.init(this, "编辑收货地址", "删除", new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                delete();
//            }
//        });
        StyleUtil.init(this, "编辑收货地址");
        mineAddressSubmit.setText("保存");
        bean = (Address) getIntent().getSerializableExtra("bean");
        if(bean == null){
            //前面值没有传递过来，延时关闭页面
            ActionUtils.delayFinish(this);
            return;
        }
        inputBox.setText(bean.getName());
        inputBox.setSelection(bean.getName().length());
        inputBox2.setText(bean.getPhone());
        inputBox3.setText(bean.getAddressDetail());
        city=bean.getAreaAddress();
        address.setText(bean.getAreaAddress());
        UrlConstants.addressIn="MineAddressEditActivity";
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        city=intent.getStringExtra("name");
        address.setText(city);
    }


    private void delete() {
        DialogUtils.confirm(this, "确认删除此地址吗？", new SuccessListener() {
            @Override
            public void success() {
                if (null != bean) {
                    mask.setVisibility(View.VISIBLE);
                    enqueue(createCallService(MineService.class).delAddress(bean.getId()), new RetrofitCallbackListener<BaseEntity>() {
                        @Override
                        public void onSuccess(Call<BaseEntity> call, BaseEntity result) {
                            mask.setVisibility(View.GONE);
                            ToastUtil.showLong(mContext, "删除成功");
                            if (bean.isDefault()) {
                                DataCacheManager.getInstance().getUser().setAddress(null);
                                DataCacheManager.getInstance().setUser(DataCacheManager.getInstance().getUser());
                            }
                            finish();
                        }

                        @Override
                        public void onError(String msg) {
                            mask.setVisibility(View.GONE);
                        }
                    });
                }
            }
        });
    }


    @OnClick({R.id.mine_address_submit,R.id.rl3,R.id.address_default})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mine_address_submit:
                // 修改提交
                String addressDetail = EditTextUtils.getTextNoSpace(inputBox3);
                String name = EditTextUtils.getTextNoSpace(inputBox);
                String phone = EditTextUtils.getTextNoSpace(inputBox2);

                if (name.length() < 2 || name.length() > 10 || !Utils.justTxt(name)) {
                    ToastUtil.showShort(mContext, "请输入2-10位收货人姓名");
                    return;
                }

                if (phone.contains("*") && bean != null) {
                    if (phone.equalsIgnoreCase(bean.getPhone()))
                        phone = bean.getPhone();
                    else {
                        ToastUtil.showShort(mContext, "请输入11位手机号");
                        return;
                    }
                }

                if (!Utils.isPhoneNum(phone)) {
                    ToastUtil.showShort(mContext, "请输入11位手机号");
                    return;
                }

                if (addressDetail.length() < 5) {
                    ToastUtil.showShort(mContext, "请输入5-150字详细地址");
                    return;
                }

                mask.setVisibility(View.VISIBLE);
                closeKeyboard();
                Map<String,String> params=new HashMap<>();
                params.put("token", SePref.getToken());
                params.put("id",bean.getId());
                params.put("name",name);
                params.put("mobile",phone);
                params.put("city",String.valueOf(city));
                params.put("address",addressDetail);
                params.put("use",isChooseDefault?"1":"0");
                closeKeyboard();
                enqueue(createCallService(HomeService.class).addressEdit(params), new RetrofitCallbackListener<BaseEntity>() {
                    @Override
                    public void onSuccess(Call<BaseEntity> call, BaseEntity result) {
                        mask.setVisibility(View.GONE);
                        finish();
                    }

                    @Override
                    public void onError(String msg) {
                        mask.setVisibility(View.GONE);
                    }
                });
                break;
            case R.id.rl3:
                qStartActivity(ChooseAddressFirstActivity.class);
                break;
            case R.id.address_default:
                isChooseDefault=!isChooseDefault;
                address_default.setCompoundDrawablesWithIntrinsicBounds(0,0,isChooseDefault?R.mipmap.check_active:R.mipmap.uncheck_active,0);
                break;
        }
    }

}
